﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Jalal.Library.DataStructure
{
    public class ReadWriteCustomCollection<TGenericType> : CustomCollection<TGenericType>, IEnumerable<TGenericType>, IList<TGenericType>
    {
        public ReadWriteCustomCollection()
        {

        }

        public ReadWriteCustomCollection(ReadOnlyCustomCollection<TGenericType> readOnlyItems)
        {
            this._items.Clear();
            foreach (TGenericType item in readOnlyItems)
            {
                this._items.Add(item);
            }
        }

        public ReadWriteCustomCollection(List<TGenericType> items)
        {
            this._items = items;
        }

        public void Add(TGenericType item)
        {
            this._items.Add(item);
        }

        public bool Remove(TGenericType item)
        {
            return this._items.Remove(item);
        }

        public TGenericType this[int index]
        {
            get { return this._items[index]; }
            set { this._items[index] = value; }
        }

        public void Clear()
        {
            this._items.Clear();
        }

        public ReadOnlyCustomCollection<TGenericType> ReadOnlyClone()
        {
            ReadOnlyCustomCollection<TGenericType> readOnlyClone =
                new ReadOnlyCustomCollection<TGenericType>(this._items);
            return readOnlyClone;
        }

        #region IEnumerable Members

        public new System.Collections.IEnumerator GetEnumerator()
        {
            return new ReadWriteCustomCollectionEnumerator<TGenericType>(this);
        }

        #endregion

        #region IEnumerable<TGenericType> Members

        IEnumerator<TGenericType> IEnumerable<TGenericType>.GetEnumerator()
        {
            return new ReadWriteCustomCollectionEnumerator<TGenericType>(this);
        }

        #endregion

        #region IList<TGenericType> Members

        public int IndexOf(TGenericType item)
        {
            return _items.IndexOf(item);
        }

        public void Insert(int index, TGenericType item)
        {
            _items.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _items.RemoveAt(index);
        }

        #endregion

        #region ICollection<TGenericType> Members


        public void CopyTo(TGenericType[] array, int arrayIndex)
        {
            _items.CopyTo(array, arrayIndex);
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        #endregion
    }
}
